﻿using Microsoft.Extensions.Configuration;
using System;
using System.Net;
using System.Threading.Tasks;

namespace RedisUserClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

			//命令行参数启动
			//dotnet RedisUserClient.dll --minute=18
			var builder = new ConfigurationBuilder();
			builder.AddCommandLine(args);
			var configuration = builder.Build();
			int minute = int.Parse(configuration["minute"]);
			
			//开启10个线程去抢购
			Console.WriteLine($"在{minute}分0秒正式开启秒杀！");
			var flag = true;
			while (flag)
			{
				//循环到老师输入的这个时间的时候，我们可以开始抢票
				if (DateTime.Now.Minute == minute)
				{
					flag = false;
					Parallel.For(0, 30, (i) =>
					{
						int temp = i;
						Task.Run(() =>
						{
							LaibaWebRequest("http://localhost:5000", temp);
						});
					});
				}
			}


			Console.ReadKey();
		}


        private static HttpWebResponse LaibaWebRequest(string address,long userId)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create($"{address}/Home/Index?userid={userId}");
            HttpWebResponse rsq = (HttpWebResponse)req.GetResponse();
            return rsq;
        }
    }
}
